package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.SensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import org.apache.hbase.thirdparty.io.netty.util.internal.ObjectUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;

/**
 * @Author: JW
 * @Description: TODO
 * @DateTime: 2022-02-16 18:15
 **/
@Service
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {
    /**
     * @param sensitiveDto
     * @Author: JW
     * @Description: TODO 敏感词查询 关键字查询
     * @Date: 19:16 2022-02-18
     * @Param: [sensitiveDto]
     * @return: com.heima.model.common.dtos.ResponseResult
     */
    @Override
    public PageResponseResult find(SensitiveDto sensitiveDto) {

        //参数校验
        if (ObjectUtils.isEmpty(sensitiveDto)){
            throw new RuntimeException("参数错误");
        }

        //分页参数
        Page<WmSensitive> page = new Page<>(sensitiveDto.getPage(),sensitiveDto.getSize());

        //查询条件
        LambdaQueryWrapper<WmSensitive> queryWrapper = Wrappers.lambdaQuery(WmSensitive.class).like(StringUtils
                .isNotBlank(sensitiveDto.getName()), WmSensitive::getSensitives, sensitiveDto.getName());


        page(page,queryWrapper);

        //组装返回数据
        PageResponseResult pageResponseResult = new PageResponseResult(sensitiveDto.getPage()
                ,sensitiveDto.getSize(),(int)page.getTotal());

        pageResponseResult.setData(page.getRecords());

        return pageResponseResult;
    }

}
